package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.ChangeSchoolDetail;
import com.xmy.cultivate.entity.Grade;
import com.xmy.cultivate.entity.Organization;
import com.xmy.cultivate.entity.Staff;
import com.xmy.cultivate.mapper.GradeMapper;
import com.xmy.cultivate.respons.SchedulingGrade;
import com.xmy.cultivate.service.ICourseSchedulingService;
import com.xmy.cultivate.service.IGradeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.service.IOrganizationService;
import com.xmy.cultivate.service.IStaffService;
import com.xmy.cultivate.util.IdWorker;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.List;

/**
 * <p>
 * 班级 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-03-20
 */
@Service
public class GradeServiceImpl extends ServiceImpl<GradeMapper, Grade> implements IGradeService {

    @Autowired
    @Lazy
    GradeMapper gradeMapper;

    @Autowired
    @Lazy
    IdWorker idWorker;

    @Autowired
    ICourseSchedulingService iCourseSchedulingService;

    @Autowired
    IStaffService iStaffService;

    @Autowired
    IOrganizationService iOrganizationService;

    @Override
    public List<SchedulingGrade> getGradeListForScheduling(Long schoolId, Integer subjectsId, Integer yearPart, Integer qurarterNum, String weekDates, Integer lessonType, Integer sourseLeve, Long adminId) {
        return gradeMapper.getGradeListForScheduling(schoolId, subjectsId, yearPart, qurarterNum, weekDates, lessonType, sourseLeve, adminId);
    }

    @Override
    public List<String> getSchoolTimeList(Long schoolId, Integer yearPart, Integer qurarterNum, String weekDates, Integer lessonType) {
        return gradeMapper.getSchoolTimeList(schoolId, yearPart, qurarterNum, weekDates, lessonType);
    }


    /**
     * 转校创建班级
     *
     * @param outGradeId
     * @return
     */
    @Override
    public Grade createdForChangeSchool(Long outGradeId, Long inSchoolId, Long teacherId, ChangeSchoolDetail changeSchoolDetail, boolean isDelOd) throws ParseException {
        try {

            Staff staff = iStaffService.getById(teacherId);

            Organization inSchool = iOrganizationService.getById(inSchoolId);

            QueryWrapper<Grade> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("out_id",outGradeId);
            queryWrapper.eq("teacher_id",teacherId);
            queryWrapper.eq("school_id",inSchoolId);
            queryWrapper.last("limit 1");
            //是否创建过
            Grade grade = this.getOne(queryWrapper);
            if (grade != null){
                return grade;
            }

            grade = this.getById(outGradeId);
            if (grade == null) {
                return null;
            }


            Staff staffOld = iStaffService.getById(grade.getTeacherId());

            Grade gradeNew = new Grade();
            BeanUtils.copyProperties(grade, gradeNew);
            gradeNew.setId(idWorker.nextId());
            gradeNew.setSchoolId(inSchoolId);
            //gradeNew.setStatus(StatusGradeEnum.NORMAL);
            gradeNew.setOutId(outGradeId);
            gradeNew.setTeacherId(teacherId);
            String gradeName = grade.getName().replace(staffOld.getName(),staff.getName());
            gradeNew.setName(gradeName);
            gradeNew.setSchoolName(inSchool.getName());

            if (this.save(gradeNew)){
                iCourseSchedulingService.changeSchoolCreate(grade,gradeNew,changeSchoolDetail,isDelOd);//创建排课,使用旧班级的排课复制出新的班级
                return gradeNew;
            }else {
                return null;
            }
        }catch (Exception e){
            return null;
        }
    }
}
